/**
 * 不要轻易修改以下内容，如需配置路由地址移步config.json
 *
 */

import Vue from 'vue'
import VueRouter from 'vue-router';
import Config from './config.json';
import store from '../store';
const registerRoute = (config) => {
    let route = [];
    config.map(page => {
        let obj = {
            name: page.name,
            path: page.path,
            component: resolve => require([`../pages${page.component}`], resolve),
            meta: page.meta
        };
        if (page.children) {
            obj.children = registerRoute(page.children);
        }
        route.push(obj)
    });
    return route;
};
Vue.use(VueRouter);
const routes = registerRoute(Config);
const router = new VueRouter({
    routes
});

router.beforeEach(({meta,path}, from, next) => {
    var {auth = false} = meta
    var isLogin = Boolean(store.state.user.id)
    if (auth && !isLogin && path !== '/login') {//用户未登录权限
        return next({
            path: '/login'
        })
    }
    document.title = meta.title || document.title;//页面标题
    next()
})
export default router;
